﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SkillRang : MonoBehaviour
{	
	public float length = 1f;//矩形长
	public float width = 1f;//矩形宽
	public float radius = 1f;//圆形半径
	public int skillType = 1;//技能类型
	private bool isShow = false;//是否处于攻击范围
	private Transform hero_a;//我方英雄
	private Transform hero_b;//敌方英雄
    // Start is called before the first frame update
    void Start()
    {
        hero_a = GameObject.Find("hero_a").transform;
        hero_b = GameObject.Find("hero_b").transform;
    }

    // Update is called once per frame
    void Update()
    {
    	ShowRang();
    }
    private void ShowRang()
    {
    	if(skillType == 1)
    	{
    		//方形区域
    		Vector3 forward = hero_a.forward;
	        Vector3 direction = hero_b.position - hero_a.position;
	        float dot = Vector3.Dot(forward,direction);
	        if (dot >0)
	        {
	        	//在前方
	        	float vertical = Vector3.Project(direction, hero_a.forward).magnitude;//垂直方向投影
	        	float horizontal = Vector3.Project(direction, hero_a.right).magnitude;//水平方向投影
	        	if (vertical <= width && horizontal <= length/2)
	        	{
	        		isShow = true;
	        		return;
	        	}
	        }
	        isShow = false;
    	} 
    	else
    	{
    		// 圆形区域
    		Vector3 pos_a = hero_a.position;
    		Vector3 pos_b = hero_b.position;
            float distence = Mathf.Sqrt((pos_a.x-pos_b.x)*(pos_a.x-pos_b.x)+(pos_a.y-pos_b.y)*(pos_a.y-pos_b.y));
            if (distence <= radius)
            {
            	isShow = true;
            }
            else
            {
            	isShow = false;
            }
    	}
    }
    private void OnDrawGizmos()
    {
        Handles.color = isShow ? Color.cyan : Color.red;

        // Vector3 cornerA = hero_a.position + Vector3.left * length * .5f;
        // Vector3 cornerB = hero_a.position + Vector3.right * length * .5f;
        // Vector3 cornerC = cornerB + Vector3.forward * width;
        // Vector3 cornerD = cornerA + Vector3.forward * width;

        // Handles.DrawLine(cornerA, cornerB);
        // Handles.DrawLine(cornerB, cornerC);
        // Handles.DrawLine(cornerC, cornerD);
        // Handles.DrawLine(cornerD, cornerA);
        Handles.CircleHandleCap(0,hero_a.position,hero_a.rotation * Quaternion.LookRotation(Vector3.up),radius,EventType.Repaint);
    }
}
